//package com.es.springbootes.scoll.service;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.es.springbootes.entity.LivingData;
//import com.es.springbootes.scoll.map.ProfileList;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchRequestBuilder;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.Index;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.rest.RestStatus;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.jsoup.Jsoup;
//import org.jsoup.nodes.Document;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.RestTemplate;
//
//import java.io.IOException;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author 邹宇杰
// * @email 2901570623@qq.com
// * @site http://zouyujie.top
// * @date 2022/2/13 14:16
// */
//@Component
//public class JsoupProfileService {
//
//    private static final Logger logger = LoggerFactory.getLogger(JsoupProfileService.class);
//
//    @Autowired
//    @Qualifier("restHighLevelClient")
//    private  RestHighLevelClient client;
//
////    @Scheduled(cron = "0 0 12 * * ?")
////    @Scheduled(cron = "* * */2 * * *")
//    @Scheduled(cron = "* * * * * *")
//    public void pullProfileData() {
//        logger.info("主播个人信息爬取任务开始");
//        try{
//            RestTemplate restTemplate = new RestTemplate();
//            int PageCount = 144;
//            //每次更新144页
//            for(int i = 0; i < PageCount; i++){
//                String livingjsondata = restTemplate.getForObject(
//                        "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&tagAll=0&callback=getLiveListJsonpCallback&page="+i,
//                        String.class);
//                String jsondata = livingjsondata.replaceAll("getLiveListJsonpCallback", "").replaceAll("[\\(\\)]", "");
//                JSONObject jsonObject = JSONObject.parseObject(jsondata);
//                JSONObject data = jsonObject.getJSONObject("data");
//                JSONArray datas = data.getJSONArray("datas");
//                List<LivingData> dataList = JSONArray.parseArray(datas.toJSONString(), LivingData.class);
//                //直播平台的数据进入mysql备份
//
//                //将直播平台的数据全部注入Es中分析
//                BulkRequest bulkRequest = new BulkRequest();
//                bulkRequest.timeout(TimeValue.timeValueMinutes(2));
//                bulkRequest.timeout("2m");
//                for (LivingData livingData : dataList) {
//                    Document document = Jsoup.connect("https://www.huya.com/" + livingData.getProfileRoom()).get();
//                    livingData.setNumber(Integer.valueOf(document.getElementById("activityCount").text()));
//                    bulkRequest.add(
//                            new UpdateRequest("livingdata", livingData.getProfileRoom())
//                                    .doc(JSONObject.toJSONString(livingData), XContentType.JSON));
//                }
//                BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//                if(bulk.hasFailures()){
//                    logger.error(bulk.buildFailureMessage());
//                }
//                logger.info("第"+i+"页的当前批量任务状态:"+bulk.status().toString());
//            }
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//    }
//
//}
